package com.logic.modular.game.player.task.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comment.pojo.domain.logic.domain.PlayerTask;
import com.comment.pojo.domain.logic.core.CoreTaskConfig;
import com.logic.modular.game.player.task.mapper.PlayerTaskMapper;
import com.logic.modular.gameCore.cultivate.task.service.CoreTaskConfigServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务定时任务
 */
@Slf4j
@Component
@EnableScheduling
public class TaskScheduledTask {
    @Resource
    private PlayerTaskMapper playerTaskMapper;

    @Async
    public void refreshTask(int type) {
        List<CoreTaskConfig> collect = CoreTaskConfigServiceImpl.listCache().stream()
                .filter(item -> item.getTaskType() == type).collect(Collectors.toList());

        LambdaQueryWrapper<PlayerTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PlayerTask::getTaskId, collect.stream().map(CoreTaskConfig::getId));

        PlayerTask playerTask = new PlayerTask();
        playerTask.setIsFinish(0);
        playerTask.setIsReceive(0);
        playerTask.setProgress(new HashMap<>());
        int update = playerTaskMapper.update(playerTask, wrapper);
        log.info("刷新任务{}成功，刷新数量：{}", type, update);
    }

    @Scheduled(cron = " 0 0 0 * * ?")
    public void dailyTasks() {
        log.info("开始刷新每日任务");
        refreshTask(1);
    }

    @Scheduled(cron = "0 0 0 1/7 * ?")
    public void seasonalTasks() {
        log.info("开始刷新每周任务");
        refreshTask(2);
    }

    @Scheduled(cron = "0 0 0 1 * ?")
    public void seasonal2Tasks() {
        log.info("开始刷新赛季任务");
        refreshTask(3);
    }
}
